<template>
  <div class="app-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
      <el-breadcrumb-item :to="{ path: '/index' }"><i class="fa fa-home"></i> 首页</el-breadcrumb-item>
      <el-breadcrumb-item>设备管理</el-breadcrumb-item>
      <el-breadcrumb-item>设备台账</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索区域 -->
    <el-form :inline="true" :model="queryParams" class="search-form" size="small">
      <el-form-item label="设备名称">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="设备型号">
        <el-input v-model="queryParams.model" placeholder="请输入设备型号" clearable style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="设备状态">
        <el-select v-model="queryParams.status" placeholder="请选择设备状态" clearable style="width: 200px;">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增设备</el-button>
      <el-button type="success" icon="el-icon-download" @click="handleExport">导出数据</el-button>
    </right-toolbar>

    <!-- 设备表格 -->
    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column label="设备ID" prop="id" width="100" align="center"></el-table-column>
      <el-table-column label="设备名称" prop="deviceName" width="180"></el-table-column>
      <el-table-column label="设备型号" prop="model" width="150"></el-table-column>
      <el-table-column label="设备编号" prop="deviceCode" width="150" align="center"></el-table-column>
      <el-table-column label="设备状态" align="center" width="120">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="购置日期" prop="purchaseDate" width="140" align="center"></el-table-column>
      <el-table-column label="责任人" prop="responsiblePerson" width="120" align="center"></el-table-column>
      <el-table-column label="存放位置" prop="location" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    ></pagination>

    <!-- 新增/编辑设备弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="form.deviceName" placeholder="请输入设备名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入设备型号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="form.deviceCode" placeholder="请输入设备编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择设备状态">
                <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="购置日期" prop="purchaseDate">
              <el-date-picker v-model="form.purchaseDate" type="date" placeholder="选择购置日期" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人" prop="responsiblePerson">
              <el-input v-model="form.responsiblePerson" placeholder="请输入责任人"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="存放位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入存放位置"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="设备描述">
              <el-input v-model="form.description" type="textarea" rows="4" placeholder="请输入设备描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 设备详情弹窗 -->
    <el-dialog title="设备详情" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="设备名称">{{ detailForm.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="设备型号">{{ detailForm.model }}</el-descriptions-item>
        <el-descriptions-item label="设备编号">{{ detailForm.deviceCode }}</el-descriptions-item>
        <el-descriptions-item label="设备状态"><dict-tag :options="statusOptions" :value="detailForm.status"></dict-tag></el-descriptions-item>
        <el-descriptions-item label="购置日期">{{ detailForm.purchaseDate }}</el-descriptions-item>
        <el-descriptions-item label="责任人">{{ detailForm.responsiblePerson }}</el-descriptions-item>
        <el-descriptions-item label="存放位置">{{ detailForm.location }}</el-descriptions-item>
        <el-descriptions-item label="设备描述">{{ detailForm.description || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailForm.createTime }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ detailForm.updateTime || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice, exportDevice } from "@/api/device"; // 请根据实际API路径调整

export default {
  name: "Device台账",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 设备台账表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        model: null,
        status: null
      },
      // 表单参数
      form: {},
      // 详情表单参数
      detailForm: {},
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "设备型号不能为空", trigger: "blur" }
        ],
        deviceCode: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "设备状态不能为空", trigger: "change" }
        ],
        purchaseDate: [
          { required: true, message: "购置日期不能为空", trigger: "change" }
        ],
        responsiblePerson: [
          { required: true, message: "责任人不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "存放位置不能为空", trigger: "blur" }
        ]
      },
      // 设备状态字典
      statusOptions: []
    };
  },
  created() {
    this.getList();
    this.getDicts("device_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询设备台账列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm("form");
      this.title = "新增设备";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.resetForm("form");
      const id = row.id || this.ids;
      getDevice(id).then(response => {
        this.form = response.data;
        this.title = "修改设备";
        this.open = true;
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getDevice(row.id).then(response => {
        this.detailForm = response.data;
        this.detailOpen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        "是否确认删除设备编号为" + ids + "的数据项?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDevice(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.download(exportDevice(queryParams), "device.xlsx");
    },
    /** 选择多条数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    }
  }
};
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
}
</style>
